// 引入接口文件
import { exportConlog, regionArea, queryProject, locationServ } from './customerMapServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
import { DateUtil } from 'utils/dateUtil'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress } from "utils/util"

const queryList = [
  {
    name: '区域', type: 'selectClear', allowClear: true, key: 'regionId', value: '', notNeedAll: true, list: []
  },
  {
    name: '城市', type: 'treeSelect', allowClear: true, key: 'city', value: '', notNeedAll: true, list: [],
  },
  {
    name: '项目', type: 'selectClear', allowClear: true, key: 'productId', value: '', notNeedAll: true, list: [],
  },
  {
    name: '地点类型', type: 'selectClear', key: 'locationType', value: '', notNeedAll: false,
    list: [
      { id: 'all', name: '用户全部地址' },
      { id: 'active', name: '用户活跃地址' },
    ],
  },
  // {
  //   name: '操作时间', type: 'time', key: ['startDate','endDate'], value: '', 
  // },
]
const sourceList1 = [
  { label: '未到访客户', checked: true, img: 'https://zmzj-pro.oss-cn-shenzhen.aliyuncs.com/web/customer/choice_c%402x.png', value: 'not_visit', key: 'not_visit' },
  { label: '已到访客户', checked: false, img: 'https://zmzj-pro.oss-cn-shenzhen.aliyuncs.com/web/customer/choice_a%402x.png', value: 'visited', key: 'visited' },
  { label: '已成交客户', checked: false, img: 'https://zmzj-pro.oss-cn-shenzhen.aliyuncs.com/web/customer/choice_b%402x.png', value: 'deal', key: 'deal' }
]
// 初始默认状态
const defultState = {
  loading: false,
  btnLoading: false,
  biz: {
    queryForm: {}
  },
  queryList,
  area1: [],
  pos: null,
  cityItem: [],
  cityInfo: {},
  center: '',
  products: [],
  markers: [],
  provinceData: [],
  province: null,
  sourceList: [
    { label: '未到访客户', checked: true, img: 'https://zmzj-pro.oss-cn-shenzhen.aliyuncs.com/web/customer/choice_c%402x.png', value: 'not_visit', key: 'not_visit' },
    { label: '已到访客户', checked: false, img: 'https://zmzj-pro.oss-cn-shenzhen.aliyuncs.com/web/customer/choice_a%402x.png', value: 'visited', key: 'visited' },
    { label: '已成交客户', checked: false, img: 'https://zmzj-pro.oss-cn-shenzhen.aliyuncs.com/web/customer/choice_b%402x.png', value: 'deal', key: 'deal' }
  ],
  checkedList: [],
  indeterminate: true,
  checkAll: false,
  startDate: DateUtil.getStartDayOfWeek(),
  endDate: DateUtil.getEndDayOfWeek(),
  map_level: 1,
  getZoom: 5,
  customerTypeList: ['not_visit'],
  productIdList: [],
  search_city: '',
  prosCity: '',
  locationType: 'all',
  searchWeek: 1,
  customerLine: 'all',
  allMIS: [],
  copygetZoom: 5,
  productIdList1: [],
  bamapsff: 0,
  isReset: false,
  showcustomerBody: true,//客户地图显隐标识
  allNum: 0,
  not_visitNum: 0,
  visitedNum: 0,
  dealNum: 0,
  hasData:false,
  customerTypearr: 'all'
}
// 声明module
const tmpModule = {
  namespace: 'customerMapModel',
  state: cloneDeep(defultState),
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname, query }) => {
        if ('' + tmpModule.pathname === '' + pathname) {
          dispatch({ type: 'regionArea', payload: {} });
          dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
          // dispatch({ type: 'typeList', payload: {} });
          let startDate1 = DateUtil.getStartDayOfWeek()
          let endDate1 = DateUtil.getEndDayOfWeek()
          // dispatch({
          //   type: 'search', payload: {
          //     startDate: startDate1,
          //     endDate: endDate1,
          //     map_level: 1,
          //     customerTypeList: ['not_visit'],
          //     locationType: 'all'
          //   }
          // });
        }
      })
    },
  },
  // 方法列表
  effects: {

    //区域
    *regionArea({ payload }, { put, call, select }) {
      let { biz, areaList, queryList } = yield select(state => state[tmpModule.namespace])
      const { data, success } = yield call(regionArea);
      if (success) {
        data.map((item, index) => {
          item.id = item.value
          item.name = item.label
        })
        queryList[0].list = data
        yield put({ type: 'updateStore', payload: { area: data, areaList, queryList } })

      }
    },
    //查询操作
    *search({ payload = {} }, { call, put, select }) {
      let { queryList, startDate, endDate, map_level, customerTypeList, search_city,
        productIdList,
        locationType } = yield select(state => state[tmpModule.namespace])
      //组装接口入参
      let params = { ...payload }
      if (!params.map_level) {
        params.map_level = map_level
      }
      yield put({ type: 'updateStore', payload: { hasData:true } })
      const { success, rows } = yield call(locationServ, params)
      if (success) {
        yield put({ type: 'updateStore', payload: { provinceData: rows,hasData:false } })
      }
    },

    // 查询条件重置
    *queryReset({ payload }, { call, put, select }) {
      let { searchWeek, startDate,
        endDate, map_level, sourceList, productIdList, search_city, locationType, getZoom, customerTypeList } = yield select(state => state[tmpModule.namespace])
      let params = { ...payload }
      let newQueryList = cloneDeep(queryList)
      let newQueryList2 = sourceList
      let productIdList1 = []
      let search_city1 = ''
      let startDate1 = startDate
      let endDate1 = endDate
      // 拖拽和缩放地图不改变以下参数
      if (!params.reset && params) {
        searchWeek = 1
        locationType = ''
        customerTypeList = ['not_visit']
        startDate1 = DateUtil.getStartDayOfWeek()
        endDate1 = DateUtil.getEndDayOfWeek()
        newQueryList2 = cloneDeep(sourceList1)
      }

      yield put({
        type: 'updateStore', payload: {
          searchWeek,
          productIdList: productIdList1,
          locationType,
          search_city: search_city1,
          customerTypeList,
          queryList: newQueryList,
          startDate: startDate1,
          endDate: endDate1,
          sourceList: newQueryList2
        }
      });
      yield put({ type: 'regionArea', payload: {} });
    },
    //改变查询条件
    *inputChange({ payload }, { call, put, select }) {
      let { queryList, getZoom, map_level, search_city, productIdList, locationType } = yield select(state => state[tmpModule.namespace])
      const { key, value } = payload;
      let data = queryList.find(item => item.key === key);
      let map_province,
        map_city,
        map_district;
      if (data.key == 'regionId') {
        yield put({ type: 'handleCityData', payload });
        search_city = ''
        productIdList = []
        // map_level=1
        // getZoom = 5
      } else if (data.key == 'city') {
        yield put({ type: 'getProject', payload });
        search_city = value
        productIdList = []
        if (value) {
          map_province = '';
          map_city = '';
          map_district = '';
        }
      } else if (data.key == 'productId') {
        productIdList = []
        if (value) {
          productIdList.push(value)
          getZoom = 12
        } else {
          if (search_city != '') {
            getZoom = 10
          } else {
            getZoom = 5
          }
        }

      } else if (data.key == 'locationType') {
        locationType = value
      }
      data.value = value;
      queryList = [...queryList, ...data]
      //刷新列表参数
      yield put({
        type: 'updateStore', payload: {
          getZoom,
          map_level,
          queryList,
          productIdList,
          search_city,
          map_province,
          map_city,
          map_district,
          locationType
        }
      });
    },
    // 处理城市数据
    *handleCityData({ payload }, { put, call, select }) {
      let { queryList, area, cityItem } = yield select(state => state[tmpModule.namespace])
      const { key, value } = payload;
      let data = queryList.find(item => item.key === key);
      area.map((i) => {
        if (i.value === value) {
          //label换成title 树选择已移除label
          i.children.map((icity) => {
            icity.title = icity.label
            icity.children.map((j) => {
              j.title = j.label
              j.value = j.label
            })
            cityItem.push(icity)
          })
        }
      })
      queryList[1].list = cityItem
      queryList[1].value = ""

      queryList[2].list = []
      queryList[2].value = ""

      queryList[1].reload = !queryList[1].reload
      yield put({ type: 'updateStore', payload: { queryList, cityItem: [] } });
    },
    //获取项目
    *getProject({ payload }, { put, call, select }) {
      let { queryList } = yield select(state => state[tmpModule.namespace])
      let { rows, success } = yield call(queryProject, payload.value);
      rows.map((item, index) => {
        item.id = item.productId
      })
      queryList[2].list = rows
      queryList[2].value = ""
      // yield put({ type: 'updateStore', payload: { products:rows}});
    },


    *exportConlog({ payload }, { put, call, select }) {
      let { queryList } = yield select(state => state[tmpModule.namespace])
      let params = { pageSize: 9999, pageNo: 1, ...queryList }
      let { data, success } = yield call(exportConlog, params)
      if (success) {
        message.success('导出成功,正在为您下载表格')
        window.location.href = data
      }
    }
  },



  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return { ...preState, ...action.payload };
    }
  }
}

export default tmpModule
